﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="OtfController.cs" company="DST Nexdox">
//   Copyright (c) DST Nexdox. All rights reserved.
// </copyright>
// <summary>
//   Defines the OtfController type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace UnityWeb.Controllers
{
  using System;
  using System.Collections.Generic;
  using System.Web.Mvc;
  using Entities;
  using Logging;
  using ServiceInterfaces;
  using UnityWeb.Constants;
  using UnityWeb.Filters;
  using UnityWeb.Models.Otf;

  [AuthorizeLoggedInUser(Roles = Roles.Admin + "," + Roles.DstAdmin + "," + Roles.Governor)]
  public class OtfController : BaseController
  {
    private const Int32 PageSize = 10;
    private readonly IAppManCoEmailService appManCoEmailService;
    private readonly IApplicationService applicationService;
    private readonly IManCoService manCoService;
    private readonly IDocTypeService docTypeService;
    private readonly IUserService userService;

    public OtfController(
        IAppManCoEmailService appManCoEmailService, 
        IApplicationService applicationService,
        IManCoService manCoService,
        IDocTypeService docTypeService,
        IUserService userService,
        ILogger logger)
        : base(logger)
    {
      this.appManCoEmailService = appManCoEmailService;
      this.applicationService = applicationService;
      this.manCoService = manCoService;
      this.docTypeService = docTypeService;
      this.userService = userService;
    }

    [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
    public ActionResult Index(Int32 page = 1)
    {
      IList<Application> applications = this.applicationService.GetApplications();
      IList<ManCo> manCos = this.manCoService.GetManCos();

      var model = new SelectAppManCoEmailsViewModel
                    {
                      Page =
                        this.TempData.ContainsKey("Page")
                          ? Convert.ToInt32(this.TempData["Page"].ToString())
                          : page
                    };

      model.AddApplications(applications);
      model.AddManCos(manCos);

      return this.View(model);
    }

    [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
    public ActionResult Show(OtfParameterModel parameterModel)
    {
      if (parameterModel.IsAjaxCall)
      {
        IList<Application> applications = this.applicationService.GetApplications();
        IList<ManCo> manCos = this.manCoService.GetManCos();
        IList<DocType> doctypes = this.docTypeService.GetDocTypes();
      
        PagedResult<AppManCoEmail> appManCoEmails = this.appManCoEmailService.GetPagedAppManCoEmails(
          parameterModel.AccountNumber, parameterModel.AppId, parameterModel.ManCoId, parameterModel.Page, PageSize);

        var model = new OtfItemsViewModel(applications, manCos, doctypes);
        model.AddAppManCoEmails(appManCoEmails);

        if (model.AppManCoEmails.Count < 1)
        {
          this.TempData["comment"] = "No records for the selected criteria";
        }
     
        return this.PartialView("_PagedOtfResults", model);
      }

      this.TempData["Page"] = parameterModel.Page;

      return this.RedirectToAction("Index", "Otf");
    }

    [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
    public ActionResult Edit(Int32 appManCoEmailId)
    {
      AppManCoEmail appManCoEmail = this.appManCoEmailService.GetAppManCoEmail(appManCoEmailId);
      IList<Application> applications = this.applicationService.GetApplications();
      IList<ManCo> manCos = this.manCoService.GetManCos();
      IList<DocType> doctypes = this.docTypeService.GetDocTypes();
      var model = new EditAppManCoEmailViewModel(applications, manCos, doctypes, appManCoEmail);

      return this.View(model);
    }

    [HttpPost]
    public ActionResult Edit(EditAppManCoEmailViewModel editAppManCoEmailViewModel)
    {
      if (ModelState.IsValid)
      {
        var loggedInUser = this.userService.GetApplicationUser();
        this.appManCoEmailService.UpdateAppManCoEmail(editAppManCoEmailViewModel.Id, editAppManCoEmailViewModel.ApplicationId, editAppManCoEmailViewModel.ManCoId, editAppManCoEmailViewModel.DocTypeId, editAppManCoEmailViewModel.OtfAccountNumber, editAppManCoEmailViewModel.OtfEmail, loggedInUser.UserName);
        return this.Json(new { Success = true });
      }
      else
      {
        this.TempData["comment"] = "Required fields are missing";
        return new RedirectResult(Request.Headers["Referer"]);
      }
    }

    [HttpPost]
    public ActionResult Create(AddAppManCoEmailViewModel model)
    {
      if (ModelState.IsValid)
      {
        this.appManCoEmailService.CreateAppManCoEmail(model.ApplicationId, model.ManCoId, model.DocTypeId, model.AccountNumber, model.Email);
        return this.RedirectToAction("Index");
      }
      else
      {
        this.TempData["comment"] = "Required files are missing";
        return new RedirectResult(Request.Headers["Referer"]);   
      }
    }

    [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
    public ActionResult Delete(Int32 appManCoEmailId)
    {
      this.appManCoEmailService.DeleteAppManCoEmail(appManCoEmailId);
      return this.Json(new { Success = true });
    }
  }
}
